<template>
	<view>
		<u--form :model="info" labelWidth="auto" ref="form1">
			<u-form-item label="标题: " prop="info.title" borderBottom ref="item1">
				<u--input v-model="info.title" border="bottom" inputAlign="center"></u--input>
			</u-form-item>
			<u-form-item label="所属企业" prop="info.enterpriseId" borderBottom @tap="showEnterprise = true;" ref="item1">
				<u--input v-model="info.tbEnterprise.name" border="bottom" inputAlign="center" disabled disabledColor="#ffffff"
					placeholder="请选择所属企业"></u--input>
				<u-icon slot="right" name="arrow-right"></u-icon>
			</u-form-item>
			<u-form-item label="最少工资(单位:k): " prop="info.minSalary" borderBottom ref="item2">
				<u--input type="numbe" v-model="info.minSalary" border="bottom" inputAlign="center"></u--input>
			</u-form-item>
			<u-form-item label="最多工资(单位:k): " prop="info.maxSalary" borderBottom ref="item2">
				<u--input type="numbe" v-model="info.maxSalary" border="bottom" inputAlign="center"></u--input>
			</u-form-item>
			<u-form-item label="需要人数: " prop="info.peopleNum" borderBottom ref="item2">
				<u--input type="numbe" v-model="info.peopleNum" border="bottom" inputAlign="center"></u--input>
			</u-form-item>
			<u-form-item label="工作城市: " prop="info.tbCity.cityName" @tap="show = true;" borderBottom ref="item3">
				<u--input v-model="info.tbCity.cityName" border="bottom" inputAlign="center" disabled
					disabledColor="#ffffff" placeholder="请选择工作城市"></u--input>
				<u-icon slot="right" name="arrow-right"></u-icon>
			</u-form-item>
			
			<u-form-item label="职业类型" prop="info.occupationId" borderBottom @tap="showOccupation = true;" ref="item1">
				<u--input v-model="info.tbOccupation.name" border="bottom" inputAlign="center" disabled disabledColor="#ffffff"
					placeholder="请选择职业类型"></u--input>
				<u-icon slot="right" name="arrow-right"></u-icon>
			</u-form-item>
			
			<u-form-item label="详细地址: " prop="info.workAddress" borderBottom ref="item3">
				<u--input v-model="info.workAddress" border="bottom" inputAlign="center"></u--input>
			</u-form-item>
			<u-form-item label="福利: " prop="info.welfareIds" @tap ="tagsShow=true" borderBottom ref="item3">
				
			</u-form-item>
			<u-form-item label="职位描述: " prop="info.desc" ref="item1">
				<u--textarea v-model="info.desc" placeholder="请输入内容"></u--textarea>
			</u-form-item>
		</u--form>

		<u-action-sheet :show="showEnterprise" :actions="enterpriseList" title="请选择所属企业" @close="showEnterprise = false"
			@select="selectEnterprise">
			
		</u-action-sheet>
		
		<u-action-sheet :show="showOccupation" :actions="occupationList" title="请选择职业类型" @close="showOccupation = false"
			@select="selectOccupation">
			
		</u-action-sheet>

		<u-picker :show="show" ref="uPicker" :columns="cityList" @confirm="confirm()" keyName="cityName"
			@change="changeCity"></u-picker>
		<view>

			<u-popup :show="tagsShow"  customStyle="width:500rpx;height:400rpx"  mode="center" @tap="close()" @open="open">
				<view>
					<u-checkbox-group v-model="checked"  @change="tagChange"  >
						<u-checkbox v-for="(item,index) in welfareList" :label="item.welfareName" :name="item.id"
							activeColor="green"></u-checkbox>
					</u-checkbox-group>
				</view>
			</u-popup>
			<u-button type="primary" @click="updateInfo()" icon="plus">保存</u-button>
			<u-button type="error" customStyle="margin-top:15px" @tap="delInfo()" icon="info">删除</u-button>
		</view>
	</view>
</template>

<script>
	import {
		Toast
	} from '../../common/common.js'
	import {
		findAllCityChild
	} from "../../api/city.js"
	import {
		ApiGetRecruitmentInfo
	} from "../../api/jobDetail.js"
	import {
		findAllWelfare,
		findByUserId,
		findAlloccupation,
		saveOrUpdateInfo,
		delInfo
	} from "../../api/enterprise.js"
	export default {
		data() {
			return {
				info: {},
				enterpriseList: [],
				showEnterprise: false,
				show: false,
				cityList: [],
				tagsShow: false,
				welfareList: [],
				checked: [],
				showOccupation:false,
				occupationList:[]
				
			}
		},
		methods: {
			updateInfo(){
				let ids = ''
				this.checked.forEach(item=>{
					if(item===item){
						ids = ids+","+item
					}
						
					
				})
				
				this.info.welfareIds = ids
				console.log(this.info.welfareIds)
				saveOrUpdateInfo(this.info).then(res=>{
					if(res.data.code==20000){
						Toast({title:'保存成功'})
						uni.navigateBack({
							url:'pageB/enterpriseList/enterpriseList'
						})
					}else{
						Toast({title:res.data.message})
					}
				})
			},
			delInfo(){
				delInfo({"id":this.info.id}).then(res=>{
					if(res.data.code==20000){
						Toast({title:res.data.message})
						uni.navigateBack({
							url:'pageB/enterpriseList/enterpriseList'
						})
					}else{
						Toast({title:res.data.message})
					}
				})
			},
			findOccupation(){
				findAlloccupation().then(res=>{
					this.occupationList = res.data.data
				})
			},
			findByUserIdFun() {
				let user = uni.getStorageSync("user");
				findByUserId({
					'id': user.id
				}).then(res => {
					console.log(res)
					this.enterpriseList = res.data.data;
				})
			},
			findAllWelfare() {
				findAllWelfare().then(res => {
					this.welfareList = res.data.data
				})
			},
			findAllCityChild() {
				findAllCityChild({}).then(res => {
					this.cityList.push(res.data.data) 
				})
			},
			changeCity(e) {},
			confirm(e) {
				this.info.addressId = e.value[0].cityId
				this.info.tbCity = e.value[0]
				this.show = false
			},
			ApiGetRecruitmentInfo() {
				ApiGetRecruitmentInfo({
					"jobId": this.info.id
				}).then(res => {
					this.info = res.data.data
					let ids =this.info.welfareIds.split(',')
					ids.forEach(item=>{
						this.checked.push(Number.parseInt(item))
					})
					console.log(this.checked)
				})
			},
			selectEnterprise(enterprise){
				this.info.tbEnterprise = enterprise
				this.info.enterpriseId = enterprise.id
			},
			open() {

			},
			close() {
				this.tagsShow = false
				console.log('close');
			},
			tagChange(e) {
				
			},
			selectOccupation(occupation){
				this.info.tbOccupation = occupation
				this.info.occupationId = occupation.id
			}

		},
		onLoad(options) {
			this.info.id = options.jobId || 1
			this.findAllCityChild()
			this.ApiGetRecruitmentInfo()
			this.findAllWelfare()
			this.findByUserIdFun()
			this.findOccupation()
		}
	}
</script>

<style>

</style>
